package myplaydog.qf.com.yimixianproject.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import myplaydog.qf.com.yimixianproject.R;
import myplaydog.qf.com.yimixianproject.bean.KindContentItemEntity;
import myplaydog.qf.com.yimixianproject.customview.ObservableScrollView;
import myplaydog.qf.com.yimixianproject.listener.ScrollViewListener;
import myplaydog.qf.com.yimixianproject.url.AppNetConfig;

@ContentView(R.layout.activity_kind_item)
public class KindItemActivity extends AppCompatActivity implements ScrollViewListener {

    private int idNub;
    private String pronotionStr;
    private int mRelativeLayoutHight;

    @ViewInject(R.id.activity_kind_item_scrollView)
    private ObservableScrollView mScrollView;


    @ViewInject(R.id.activity_kind_head_ll)
    private LinearLayout mLinearLayout;
    @ViewInject(R.id.activity_kind_item_rl)
    private RelativeLayout mRelativeLayout;
    @ViewInject(R.id.activity_kind_item_vPager)
    private ViewPager viewPager;
    @ViewInject(R.id.activity_kind_item_promotion)
    private ImageView promotion;
    @ViewInject(R.id.activity_kind_item_good_live)
    private ImageView good_live;
    @ViewInject(R.id.activity_kind_item_name)
    private TextView name;
    @ViewInject(R.id.activity_kind_productby)
    private TextView productby;
    @ViewInject(R.id.activity_kind_price)
    private TextView price;
    @ViewInject(R.id.activity_kind_old_price)
    private TextView old_price;
    @ViewInject(R.id.activity_kind_item_content)
    private ImageView content;
    private static final int[] goodId ={R.drawable.good_level_0,R.drawable.good_level_1,R.drawable.good_level_2,R.drawable.good_level_3,R.drawable.good_level_4};
    public void setData(KindContentItemEntity.GoodDetailBean datas) {
        KindContentItemEntity.GoodDetailBean.GoodBean data = datas.getGood();
        Log.e("----333-----",String.format(AppNetConfig.URL_KID_ICON,data.getFuntion_imgurl()));
        Log.e("----3343-----",pronotionStr+"1111");
        x.image().bind(promotion,String.format(AppNetConfig.URL_KID_ICON,pronotionStr));
        name.setText(data.getName());
        productby.setText(data.getUnit_desc()+" 产地："+data.getProductby());
        price.setText("￥"+data.getUnit_price());
        if (!TextUtils.isEmpty(data.getUnit_old_price())){
            old_price.setText("￥"+data.getUnit_old_price());
        }else {
            old_price.setVisibility(View.GONE);
        }
        x.image().bind(content,String.format(AppNetConfig.URL_KID_ICON,data.getFuntion_imgurl()));
        good_live.setImageResource(goodId[datas.getGoods_level()]);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        idNub = getIntent().getIntExtra("id",0);
        pronotionStr=getIntent().getStringExtra("str");
        Log.e("----000-----",pronotionStr+"1111");
        Log.e("---------",idNub+"");
        if (idNub!=0){
            loadData();
        }
        //获取relativelayout高度
       getHight();
        setListener();
    }

    private void setListener() {
        mScrollView.setScrollViewListener(this);
    }


    private void loadData() {
        RequestParams params =new RequestParams(String.format(AppNetConfig.URL_KID_TIAOZHUAN,idNub));
        x.http().get(params, new Callback.CacheCallback<String>() {

            @Override
            public void onSuccess(String result) {
                Gson gson =new Gson();
                KindContentItemEntity entity =gson.fromJson(result,KindContentItemEntity.class);
                KindContentItemEntity.GoodDetailBean items =entity.getGood_detail();
                Log.e("---222------",result);
                setData(items);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e("-----111----","error");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public boolean onCache(String result) {
                return false;
            }
        });
    }
    @Override
    public void finish() {
        overridePendingTransition(R.anim.activity_right_close,0);
        super.finish();

    }


    @Override
    public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
        if (scrollView==mScrollView){
            if (y>=mRelativeLayoutHight){
                mLinearLayout.setVisibility(View.VISIBLE);
            }else {
                mLinearLayout.setVisibility(View.GONE);
            }

        }
    }

    public void getHight() {
//        ViewTreeObserver vto =mRelativeLayout.getViewTreeObserver();
//        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//            @Override
//            public void onGlobalLayout() {
//                mRelativeLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//            }
//        });
        int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        mRelativeLayout.measure(w, h);
        mRelativeLayoutHight=mRelativeLayout.getMeasuredHeight();
       // int width =mRelativeLayout.getMeasuredWidth();
    }

    public void ViewClick(View view) {
        if (view!=null){
            switch (view.getId()){
                case R.id.activity_kind__add_back:
                    finish();
                    break;
                case R.id.activity_kind__share:
                    Toast.makeText(KindItemActivity.this, "分享成功", Toast.LENGTH_SHORT).show();
                    break;
                case R.id. activity_kind__btn_cart_new:
                    startActivity(new Intent(this,CartActivity.class));
                    overridePendingTransition(R.anim.activity_open,0);
                    break;
                case R.id.activity_kind__btn_cart_shop:
                    finish();
                    break;
                case R.id.activity_kind__add_cart:
                    addButton.setVisibility(View.GONE);
                    addLLayout.setVisibility(View.VISIBLE);
                    break;
                case R.id.activity_kind__add_plus:
                    finish();
                    break;
                case R.id.activity_kind__add_minus:
                    finish();
                    break;

            }
        }
    }
    @ViewInject(R.id.activity_kind__add_ll)
    private LinearLayout addLLayout;
    @ViewInject(R.id.activity_kind__add_cart)
    private Button addButton;
}
